<?php
/**
 * 装修标签
 * User: Administrator
 * Date: 2019/4/15/015
 * Time: 16:45
 */

namespace app\admin\controller;


use app\Lib\Api\Decoration\addHouseType;
use app\Lib\Api\Decoration\addStyle;
use app\Lib\Api\Decoration\editHouseType;
use app\Lib\Api\Decoration\editStyle;
use app\Lib\Api\Decoration\getHouseInfo;
use app\Lib\Api\Decoration\getHouseTypeAjaxList;
use app\Lib\Api\Decoration\getHouseTypeAjaxPage;
use app\Lib\Api\Decoration\getStyleAjaxList;
use app\Lib\Api\Decoration\getStyleAjaxPage;
use app\Lib\Api\Decoration\getStyleInfo;
use app\Lib\TopClient;
use think\Url;

class Decoration extends Base
{
    /**
     * 装修风格列表
     * @return
     */
    public function styleIndex(){
        return $this->fetch();
    }

    public function ajaxList(){
        try{
            $page = input('page');
            $limit = input('limit');
            $model = new getStyleAjaxList();
            $model->setArgs('page',$page);
            $model->setArgs('limit',$limit);
            $result = (new TopClient($model))->getSuccessData();
            $this->assign('result',$result);
            return $this->fetch('/decoration/ajaxList');
        }catch (\Exception $e){
            echo  $e->getMessage();
        }
    }

    /**
     * ajax 获取页数
     * @return array|string
     * @throws \Exception
     */
    public function ajaxPage(){
        try{
            $model =  new getStyleAjaxPage();
            $result = (new TopClient($model))->getSuccessData();
            $this->assign('result',$result);
            return $result;
        }catch (\Exception $e){
            echo  $e->getMessage();
        }
    }

    /**
     * 添加风格
     * @return array|mixed
     */
    public function addStyle(){
        try{
            if($this->request->isAjax()){
                $post = input('post.');
                if($post['id']<=0 || empty($post['id'])){
                    $this->addStyleOption($post);
                }else{
                    $this->editStyle($post);
                }
                return ['code'=>1,'msg'=>'操作成功','url'=>Url::build('admin/Decoration/styleIndex')];
            }else{
                $id =  input('menu_id',0);
                $info = $this->getStyleInfo($id);
                $this->assign('info',$info);
                return $this->fetch();
            }
        }catch (\Exception $e){
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }
    }


    /**
     *添加装修风格
     * @param $post
     * @throws \Exception
     */
    private function addStyleOption($post){
        $model = new addStyle();
        $model->setArgs('name',$post['name']);
        $model->setArgs('imgId',$post['img_id']);
        $model->setArgs('opUid',$this->login_uid);
        $result = new TopClient($model);
        $result->getSuccessData();
        return true;
    }

    /**
     * 修改风格
     * @param $post
     * @return bool
     * @throws \Exception
     */
    private function editStyle($post){
        $model = (new editStyle());
        $model->setArgs('name',$post['name']);
        $model->setArgs('opUid',$this->login_uid);
        $model->setArgs('id',$post['id']);
        $result = new TopClient($model);
        $result->getSuccessData();
        return true;
    }


    public function houseTypeIndex(){
        return $this->fetch();
    }

    public function houseTypeAjaxList(){
        try{
            $page = input('page');
            $limit = input('limit');
            $model = new getHouseTypeAjaxList();
            $model->setArgs('page',$page);
            $model->setArgs('limit',$limit);
            $result = (new TopClient($model))->getSuccessData();
//            dump($result);
            $this->assign('result',$result);
            return $this->fetch('/decoration/ajaxList');
        }catch (\Exception $e){
            echo  $e->getMessage();
        }
    }

    public function houseTypeAjaxPage(){
        try{
            $model =  new getHouseTypeAjaxPage();
            $result = (new TopClient($model))->getSuccessData();
            $this->assign('result',$result);
            return $result;
        }catch (\Exception $e){
            echo  $e->getMessage();
        }
    }

    public function addHouseType(){
        try{
            if($this->request->isAjax()){
                $post = input('post.');
                if($post['id']<=0 || empty($post['id'])){
                    $this->addHouseTypeOption($post);
                }else{
                    $this->editHouseTypeOption($post);
                }
                return ['code'=>1,'msg'=>'操作成功','url'=>Url::build('admin/Decoration/styleIndex')];
            }else{
                $id =  input('menu_id',0);
                $info = $this->getHouseTypeInfo($id);
//                dump($info);
                $this->assign('info',$info);
                return $this->fetch();
            }
        }catch (\Exception $e){
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }
    }

    /**
     *
     * @param $post
     * @return bool
     * @throws \Exception
     */
    private function addHouseTypeOption($post){
        $model = new addHouseType();
        $model->setArgs('name',$post['name']);
        if(isset($post['img_id'])){
            $model->setArgs('imgId',$post['img_id']);
        }
        $model->setArgs('opUid',$this->login_uid);
        $result = new TopClient($model);
        $result->getSuccessData();
        return true;
    }

    /**
     *
     * @param $post
     * @return bool
     * @throws \Exception
     */
    private function editHouseTypeOption($post){
        $model = new editHouseType();
        $model->setArgs('name',$post['name']);
        $model->setArgs('id',$post['id']);
        if(isset($post['img_id'])){
            $model->setArgs('imgId',$post['img_id']);
        }
        $model->setArgs('opUid',$this->login_uid);
        $result = new TopClient($model);
        $result->getSuccessData();
        return true;
    }

    /**
     * @param $id
     * @return array|string
     * @throws \Exception
     */
    private function getStyleInfo($id){
        if($id == 0 || empty($id)){
            $data = [
                'id'=>0,
                'tagName'=>'',
                'imgId'=>'',
                'imgUrl'=>'',
                'imgName'=>'',
            ];
            return $data;
        }
        $model = new getStyleInfo();
        $model->setArgs('id',$id);
        return (new TopClient($model))->getSuccessData();
    }

    /**
     * @return array|string
     * @throws \Exception
     */
    private function getHouseTypeInfo($id){
        if($id == 0 || empty($id)){
            $data = [
                'id'=>0,
                'tagName'=>'',
            ];
            return $data;
        }
        $model = new getHouseInfo();
        $model->setArgs('id',$id);
        return (new TopClient($model))->getSuccessData();
    }

}